package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.AccountConfigExtConstant;
import com.tbit.uqbike.client.constant.BrandConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.pojo.dto.AccountCustomerServiceConfigDTO;
import com.tbit.uqbike.client.pojo.newEntity.AccountConfigExt;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 代理商信息
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private MpBrandConfigService mpBrandConfigService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineBorrowService machineBorrowService;
    @Autowired
    private CacheService cacheService;

    /**
     * 根据设备编号查询代理商信息
     *
     * @param token     令牌
     * @param machineNO 设备编号
     * @param session   会话
     * @param response  响应
     * @ignoreParams request
     */
    @RequestMapping("/getByMachineNO")
    public void getByMachineNO(String token, String machineNO, HttpSession session,
                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.hasLength(machineNO, "-30001", "machineNO不能为空");

            Machine machine = machineService.getByUserCode(machineNO);
            if (machine == null) {
                throw new BaseException("-30005", "该编号不存在");
            }

            Account account = accountService.getByAccountId(machine.getAccountId());

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, account);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 查询区域客服电话
     *
     * @param token       令牌
     * @param accountId   帐户id
     * @param adAccountId 区域id
     * @param session     会话
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/getPhone")
    public void getPhone(String token, Integer accountId, Integer adAccountId, HttpSession session,
                         HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {

            AdPhoneEntity adPhoneEntity = new AdPhoneEntity();
            if (token != null) {
                User user = tokenService.getUser(token);
                if(user!=null){
                    MachineBorrow machineBorrow = machineBorrowService.getByUserId(user.getUserId());
                    if (machineBorrow != null) {
                        Machine machine = machineService.getByMachineId(machineBorrow.getMachineId());
                        if (machine == null) {
                            throw new BaseException("-30005", "该编号不存在");
                        }
                        /**区域代理客服数据*/
                        Account account = accountService.getByAccountId(machine.getAccountId());
                        if (account != null) {
                            adPhoneEntity.setAdName(account.getName());
                            adPhoneEntity.setAdPhone(account.getPhone());
                        }
                    } else {
                        /**品牌客服数据*/
                        Account account = accountService.getByAccountId(user.getAccountId());
                        if (account != null) {
                            adPhoneEntity.setName(account.getName());
                            adPhoneEntity.setPhone(account.getPhone());
                        }
                    }
                }
            } else {
                Assert.notNull(accountId, "-10001", "代理id不能为空");
                /**品牌客服数据*/
                Account account = accountService.getByAccountId(accountId);
                if (account != null) {
                    adPhoneEntity.setName(account.getName());
                    adPhoneEntity.setPhone(account.getPhone());
                }
            }
            if (adAccountId != null) {
                Account account = accountService.getByAccountId(adAccountId);
                if (account != null) {
                    adPhoneEntity.setAdName(account.getName());
                    adPhoneEntity.setAdPhone(account.getPhone());
                }
            }
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, adPhoneEntity);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


    /**
     * 获取品牌配置聚合支付
     *
     * @param token
     * @param accountId 品牌id
     * @param session   会话
     * @param response  响应
     * @ignoreParams request
     */
    @RequestMapping("/getConfig")
    public void getConfig(String token, Integer accountId, HttpSession session,
                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(accountId, "-10001", "accountId不能为空");

            BrandRemoteConfig brandRemoteConfig = cacheService.getBrandRemoteConfig(accountId,
                    BrandConstant.ADAPAY_BUTTON);
            Integer button = 0;
            if (brandRemoteConfig != null) {
                button = Integer.valueOf(brandRemoteConfig.getParamKV().substring(brandRemoteConfig.getParamKV().indexOf("=") + 1));
            }
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, button);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 获取区域配置信息
     *
     * @param accountId
     * @param session
     * @param request
     * @param response
     */
    @RequestMapping("/getAccountConfigExt")
    public void getAccountConfigExt(Integer accountId, HttpSession session, HttpServletRequest request,
                                    HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(accountId, "-10001", "accountId不能为空");

            List<AccountConfigExt> accountConfigExts = cacheService.getAccountConfigExt(accountId);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, accountConfigExts);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


    /**
     * 获取品牌配置信息
     *
     * @param accountId 品牌id
     * @param session
     * @param request
     * @param response
     */
    @RequestMapping("/getBrandConfig")
    public void getBrandConfig(Integer accountId, HttpSession session, HttpServletRequest request,
                               HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(accountId, "-10001", "accountId不能为空");

            List<BrandRemoteConfig> brandRemoteConfig = cacheService.getBrandConfig(accountId);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, brandRemoteConfig);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 获取手机登录 配置
     * @param accountId
     * @param request
     * @param response
     */
    @GetMapping("/getPhoneLogin")
    public void getPhoneLogin(Integer accountId,HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(accountId, "-10001", "accountId不能为空");
            BrandRemoteConfig brandRemoteConfig = cacheService.getBrandRemoteConfig(accountId, BrandConstant.PHONE_LOGIN);
            String paramKV = Optional.ofNullable(brandRemoteConfig).orElse(new BrandRemoteConfig(accountId, BrandConstant.PHONE_LOGIN, new Date(), "phoneLogin=1", "默认开启手机号验证码登录")).getParamKV();
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, Objects.equals(paramKV,"phoneLogin=1"));
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * @description: 获取当前所在账户(区域或品牌)的在线客服开关
     * @author: mo.shanyong
     * @date: 2024/11/27 9:55
     * @param: accountId 账户id（区域id、品牌Id）
     * @param: request
     * @param: response
     */
    @RequestMapping("/getOnlineServiceSwitch")
    public void getOnlineServiceSwitch(Integer accountId, HttpServletRequest request,
                                 HttpServletResponse response){
        Assert.notNull(accountId, "-10001", "accountId不能为空");
        AccountCustomerServiceConfigDTO serviceServiceTel = accountService.getOnlineService(accountId);
        Map<String, Object> data = new HashMap<>();
        if (Objects.nonNull(serviceServiceTel)) {
            data.put(AccountConfigExtConstant.ONLINE_SERVICE_SWITCH_KEY, serviceServiceTel.getOnlineServiceSwitch());
            data.put(AccountConfigExtConstant.ONLINE_SERVICE_URL_KEY, serviceServiceTel.getOnlineServiceUrl());
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(ResponseCode.RET, ResponseCode.SUCC);
        map.put(ResponseCode.DATA, data);
        OutputUtil.renderText(request, response, map);
    }

    /**
     * @description: 获取客服电话
     * @author: mo.shanyong
     * @date: 2024/11/27 10:20
     * @param: accountId 账户id（区域id、品牌Id）
     * @param: request
     * @param: response
     */
    @RequestMapping("/getCustomerServiceTel")
    public void getCustomerServiceTel(Integer accountId, HttpServletRequest request,
                                      HttpServletResponse response){
        Assert.notNull(accountId, "-10001", "accountId不能为空");
        AccountCustomerServiceConfigDTO serviceServiceTel = accountService.getServiceTel(accountId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(ResponseCode.RET, ResponseCode.SUCC);
        map.put(ResponseCode.DATA, serviceServiceTel);
        OutputUtil.renderText(request, response, map);

    }

}